<template>
  <el-dialog title="新增员工" :visible="showDialog" width="30%" @close="close">
    <!-- 表单 -->
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">

        <el-cascader
          v-model="form.departmentName"
          placeholder="请选择部门"
          :options="list"
          :props="defaultProps"
          @focus="getDepartmentList"
        />

      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in hireType.slice(0, 2)"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button size="small" @click="close">取消</el-button>
      <el-button type="primary" size="small" @click="addEmployee">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { getDepartmentList } from '@/api/departments'
import { listToTree } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空' },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户姓名为2-4位' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间' }
        ]
      },
      hireType: EmployeeEnum.hireType,
      list: [],
      // 指定级联菜单
      defaultProps: {
        label: 'name',
        value: 'name',
        children: 'children',
        // hover的方式展开子元素
        expandTrigger: 'hover'
      }
    }
  },
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    },
    async getDepartmentList() {
      console.log('获取部门数据')
      const data = await getDepartmentList()
      console.log(data)
      // 把部门数据转成树状数据
      this.list = listToTree(data.depts)
      console.log(this.list)
    },
    async addEmployee() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      await addEmployee({
        ...this.form,
        departmentName: this.form.departmentName.slice(-1)[0]
      })
      this.$message.success('添加成功')
      // 重新渲染列表
      this.$parent.getEmployeeList()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>

